package com.example.opengldemo.jbulletJava;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import android.opengl.GLES30;
import android.view.View;

import com.example.opengldemo.util.MatrixState;

public class Circle {
	
	int mProgram;
    int muMVPMatrixHandle;
    int muMMatrixHandle;
    int uTexHandle;
    
    int maCameraHandle;
    int maPositionHandle;
    int maTexCoorHandle;
    
	
	private FloatBuffer   vertexBuffer;
	private FloatBuffer   textureBuffer;
    int vCount=0;
    float angdegSpan;
    float xAngle=0;
    float yAngle=0;
    float zAngle=0;
	public Circle(float r, int n,float[] normal,int mProgram) {
		this.mProgram=mProgram;
		angdegSpan=360.0f/n;
		vCount=3*n;
		
		float[] vertices=new float[vCount*3];
		float[] textures=new float[vCount*2];
		float[] normals = new float[vCount*3];

		int count=0;
		int stCount=0;
		for(int i=0;i<vCount;i++){
			normals[3*i]=normal[0];
			normals[3*i+1]=normal[1];
			normals[3*i+2]=normal[2];
		}
		for(float angdeg=0;Math.ceil(angdeg)<360;angdeg+=angdegSpan)
		{
			double angrad=Math.toRadians(angdeg);
			double angradNext=Math.toRadians(angdeg+angdegSpan);

			vertices[count++]=0;
			vertices[count++]=0; 
			vertices[count++]=0;
			
			textures[stCount++]=0.5f;
			textures[stCount++]=0.5f;

			vertices[count++]=(float) (-r*Math.sin(angrad));
			vertices[count++]=(float) (r*Math.cos(angrad));
			vertices[count++]=0;
			
			textures[stCount++]=(float) (0.5f-0.5f*Math.sin(angrad));
			textures[stCount++]=(float) (0.5f-0.5f*Math.cos(angrad));

			vertices[count++]=(float) (-r*Math.sin(angradNext));
			vertices[count++]=(float) (r*Math.cos(angradNext));
			vertices[count++]=0;
			
			textures[stCount++]=(float) (0.5f-0.5f*Math.sin(angradNext));
			textures[stCount++]=(float) (0.5f-0.5f*Math.cos(angradNext));
		}
		ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
        vbb.order(ByteOrder.nativeOrder());
        vertexBuffer = vbb.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);
        

        ByteBuffer cbb = ByteBuffer.allocateDirect(textures.length*4);
        cbb.order(ByteOrder.nativeOrder());
        textureBuffer = cbb.asFloatBuffer();
        textureBuffer.put(textures);
        textureBuffer.position(0);
	}

    public void intShader(View mv)
    {

        maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");

        maTexCoorHandle=GLES30.glGetAttribLocation(mProgram, "aTexCoor");  

        muMVPMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMVPMatrix");   

        muMMatrixHandle = GLES30.glGetUniformLocation(mProgram, "uMMatrix");  

        maCameraHandle=GLES30.glGetUniformLocation(mProgram, "uCamera"); 
        uTexHandle=GLES30.glGetUniformLocation(mProgram, "sTexture"); 
    }
	
    public void drawSelf(int texId) 
    {        

    	 GLES30.glUseProgram(mProgram);

         GLES30.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);

         GLES30.glUniformMatrix4fv(muMMatrixHandle, 1, false, MatrixState.getMMatrix(), 0);  

         GLES30.glUniform3fv(maCameraHandle, 1, MatrixState.cameraLocationFB);
         

         GLES30.glVertexAttribPointer        
         (
         		maPositionHandle,   
         		3, 
         		GLES30.GL_FLOAT, 
         		false,
                3*4, 
                vertexBuffer   
         );       

         GLES30.glVertexAttribPointer  
         (  
        		maTexCoorHandle,  
         		2, 
         		GLES30.GL_FLOAT, 
         		false,
                2*4,   
                textureBuffer
         );   

         GLES30.glEnableVertexAttribArray(maPositionHandle);  
         GLES30.glEnableVertexAttribArray(maTexCoorHandle);  

         GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
         GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texId);    
         GLES30.glUniform1i(uTexHandle, 0);
           

         GLES30.glDrawArrays(GLES30.GL_TRIANGLES, 0, vCount); 
    }
}
